package com.fjh.shop.marketing.service.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjh.shop.common.PageResult;
import com.fjh.shop.common.Result;
import com.fjh.shop.marketing.model.entity.Coupon;
import com.fjh.shop.marketing.service.service.CouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2025-07-16
 */
@RestController
@RequestMapping("/coupon")
@Tag(name = "优惠券管理", description = "优惠券的CRUD操作")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @PostMapping
    @Operation(summary = "新增优惠券", description = "创建一张新的优惠券")
    public Result<Coupon> addCoupon(@RequestBody Coupon coupon) {
        Coupon savedCoupon = couponService.addCoupon(coupon);
        return Result.success(savedCoupon);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除优惠券", description = "根据ID删除优惠券")
    public Result<Boolean> deleteCoupon(@PathVariable @Parameter(description = "优惠券ID") Long id) {
        boolean result = couponService.deleteCoupon(id);
        return Result.success(result);
    }

    @PutMapping
    @Operation(summary = "更新优惠券", description = "更新优惠券信息")
    public Result<Coupon> updateCoupon(@RequestBody Coupon coupon) {
        Coupon updatedCoupon = couponService.updateCoupon(coupon);
        return Result.success(updatedCoupon);
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取优惠券详情", description = "根据ID获取优惠券详情")
    public Result<Coupon> getCouponById(@PathVariable @Parameter(description = "优惠券ID") Long id) {
        Coupon coupon = couponService.getCouponById(id);
        return Result.success(coupon);
    }

    @GetMapping("/page")
    @Operation(summary = "分页查询优惠券", description = "分页查询优惠券，支持多条件查询")
    public PageResult<Coupon> pageCoupon(
            @RequestParam(defaultValue = "1") @Parameter(description = "当前页码") Integer current,
            @RequestParam(defaultValue = "10") @Parameter(description = "每页记录数") Integer pageSize,
            @RequestParam(required = false) @Parameter(description = "优惠券ID") Long id,
            @RequestParam(required = false) @Parameter(description = "优惠券名称") String name,
            @RequestParam(required = false) @Parameter(description = "开始时间") String startTime,
            @RequestParam(required = false) @Parameter(description = "结束时间") String endTime,
            @RequestParam(required = false) @Parameter(description = "优惠券状态") Integer status
    ) {
        IPage<Coupon> page = couponService.pageCoupon(
                current, pageSize, id, name, startTime, endTime, status);
        return PageResult.success(page);
    }
}
